<?php

namespace app\app\controller;

use app\app\model\ProjectBudgetModel;
use app\app\model\ProjectModel;
use app\BaseController;


class ProjectBudget extends BaseController
{
    /**
     * 财务预算列表数据
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function list()
    {
        $page_size = input('pageSize', '20');
        $project_name = input('projectName', '');
        $where = "1=1";
        if ($project_name) $where .= " AND project_name LIKE '%" . $project_name . "%'";
        $list = ProjectBudgetModel::where($where)->paginate($page_size);
        $this->success('success', $list);
    }

    /**
     * 财务预算统计数据
     * @return void
     */
    public function analysis()
    {
        $project_name = input('projectName', '');
        $where = "1=1";
        if ($project_name) $where .= " AND project_name LIKE '%" . $project_name . "%'";
        $project_budget_total = ProjectBudgetModel::where($where)->sum('project_budget');
        $project_amount_total = ProjectBudgetModel::where($where)->sum('project_amount');
        $fee = round($project_amount_total / $project_budget_total * 100, 2);
        $info = array(
            'project_budget_total' => round($project_budget_total, 2),
            'project_amount_total' => round($project_amount_total, 2),
            'fee' => $fee
        );
        $this->success('success', $info);
    }

    /**
     * 工作台根据项目获取财务预算
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {
        $project_id = input('project_id', '');
        $project_info = ProjectModel::where('id', $project_id)->find();
        $list = ProjectBudgetModel::alias('a')->where('a.project_id', $project_info['link_project_id'])->find();
        $this->success('success', $list);
    }


}
